package com.gx.auth.dao;

import com.gx.auth.common.BaseDAO;
import com.gx.auth.entity.Role;
import com.gx.auth.entity.RoleFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2017/4/4.
 */
@Slf4j
public class RoleDAO extends BaseDAO {
    private class RoleMapper implements RowMapper {
        @Override
        public Role mapRow(ResultSet resultSet, int i) throws SQLException {
            Role role = new Role();
            role.setId(resultSet.getLong("id"));
            role.setName(resultSet.getString("name"));
            return role;
        }
    }


    /**
     * 根据id 查询角色
     * @param id
     * @return
     */
    public Role findRoleById(Long id) {
        String sql = "select * from auth_role where id = ? ";
        return (Role) jdbcTemplate.queryForObject(sql, new Object[]{id}, new RoleMapper());
    }


    /**
     * 保存角色
     * @param role
     */
    public void insertRole(Role role) {
        String sql = "insert  auth_role (name) values(?) ";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        //
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, role.getName());
                return ps;
            }
        }, keyHolder);
        role.setId(keyHolder.getKey().longValue());
    }


    /**
     * 根据id删除角色
     * @param roleId
     * @return
     */
    public int deleteRole(Long roleId) {
        String sql = "delete from auth_role where id = ? ";
        return jdbcTemplate.update(sql, roleId);
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    public int updateRole(Role role) {
        String sql = "update  auth_role set name = ? where id = ? ";
        return jdbcTemplate.update(sql, role.getName(), role.getId());
    }

    /**
     * 根据id集合批量查询
     * @param ids
     * @return
     */
    public List<Role> selectRoles(Collection<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        StringBuilder sql = new StringBuilder("select * from auth_role where id in (");
        Object[] objects = new Object[ids.size()];
        AtomicInteger index = new AtomicInteger();
        ids.forEach((id) -> {
            sql.append("?,");
            objects[index.getAndIncrement()] = id;
        });
        sql.deleteCharAt(sql.length() - 2);
        sql.append(")");
        return jdbcTemplate.query(sql.toString(), objects, new RoleMapper());
    }

    /**
     * 分页查询角色信息
     * @param page
     * @param size
     * @return
     */
    public List<Role> selectRoles(int page ,int size) {
        String sql = "select *  from auth_role limit ?,?";
        return jdbcTemplate.query(sql,new Object[]{(page-1)*size,size},new RoleMapper());
    }
}
